package org.jeese.common.config;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description:Redis缓存配置类
 * @Package:org.jeese.common.config
 * @author:wubc
 * @version:1.0
 * @date:2018年2月9日-下午5:00:50
 * @Copyright:2018 Inc. All rights reserved.
 */
@Configuration
@EnableCaching
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60)
public class RedisConfig extends CachingConfigurerSupport {
	private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
	
	@Bean
	public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
	            sb.append(target.getClass().getName());
	            sb.append(method.getName());
	            for (Object obj : params) {
	                sb.append(obj.toString());
	            }
	            return sb.toString();
			}
        };
    }
	
	@Bean
	@ConfigurationProperties(prefix = "spring.redis")
	public JedisPoolConfig getRedisConfig() {
		return new JedisPoolConfig();
	}

	@Bean(name = "redisConnectionFactory")
	public JedisConnectionFactory redisConnectionFactory() {
		JedisConnectionFactory factory = new JedisConnectionFactory();
		factory.setUsePool(true); 
		factory.setPoolConfig(getRedisConfig());
		logger.info("JedisConnectionFactory bean init success.");
		return factory;
	}

	@Bean
	public RedisTemplate<String, String> redisTemplate() {
		//RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
		//redisTemplate.setConnectionFactory(redisConnectionFactory());
		// 使用Jackson2JsonRedisSerialize 替换默认序列化
		// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
		// Jackson2JsonRedisSerializer(Object.class);
		//
		// ObjectMapper objectMapper = new ObjectMapper();
		// objectMapper.setVisibility(PropertyAccessor.ALL,
		// JsonAutoDetect.Visibility.ANY);
		// objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		//
		// jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
		//
		// // 设置value的序列化规则和 key的序列化规则
		// redisTemplate.setKeySerializer(new StringRedisSerializer());
		// redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
		// redisTemplate.afterPropertiesSet();
		return new StringRedisTemplate(redisConnectionFactory());
	}

	/**
	 * redis缓存管理器
	 */
	 @Bean
	 public RedisCacheManager  redisCacheManager() {
		 RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
		 // 设置缓存过期时间
		 cacheManager.setDefaultExpiration(1800);
		 return cacheManager;
	 }
	 

}
